import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextPane;


public class AddSubjectDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();

	/**
	 * Create the dialog.
	 */
	public AddSubjectDialog(final JComboBox comboBox) {
		setResizable(false);
		setBounds(100, 100, 361, 121);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		JLabel lblPleaseEnterName = new JLabel("Subject Name:");
		lblPleaseEnterName.setBounds(10, 18, 112, 20);
		contentPanel.add(lblPleaseEnterName);
		
		final JTextPane textPane = new JTextPane();
		textPane.setBounds(103, 18, 235, 20);
		contentPanel.add(textPane);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent arg0) {
						if (textPane.getText().length() > 0){
							File subjectDirectory = new File ("subjects/" + textPane.getText());
							subjectDirectory.mkdir();
							comboBox.addItem(textPane.getText());
							comboBox.setSelectedIndex(comboBox.getItemCount() - 1);
							AddSubjectDialog.this.dispose();
						} else {
							JOptionPane.showMessageDialog(AddSubjectDialog.this, "No subject name has been entered. Please enter subject name.", "Subject Name Not Entered", JOptionPane.INFORMATION_MESSAGE);
						}
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent arg0) {
						AddSubjectDialog.this.dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setVisible(true);
	}
}
